//
//  RCTabBarController.m
//  RCsinaMicroBlog
//
//  Created by Mr.Ray on 15/1/28.
//  Copyright (c) 2015年 Ray.peng. All rights reserved.
//

#import "RCTabBarController.h"
#import "RCtabBar.h"
#import "RCNaviegationController.h"
#import "RCHomeViewController.h"
#import "RCDiscoverViewController.h"
#import "RCMessageViewController.h"
#import "RCProfileViewController.h"

@interface RCTabBarController ()<RCtabBarDelegate>

@property (nonatomic ,weak)RCtabBar *customTabBar;

@end

@implementation RCTabBarController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self setup];
    }
    return self;
}


- (void)setup{
    
    UIViewController *home = [self addViewControllerWithClass:[RCHomeViewController class] title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    UIViewController *message = [self addViewControllerWithClass:[RCMessageViewController class] title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
    UIViewController *discover = [self addViewControllerWithClass:[RCDiscoverViewController class] title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
    UIViewController *profile = [self addViewControllerWithClass:[RCProfileViewController class] title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];

}


- (void)viewDidLoad{
    [super viewDidLoad];
    RCtabBar *tb = [[RCtabBar alloc]init];
    tb.frame = self.tabBar.frame;
    self.customTabBar = tb;
    [self.view addSubview:tb];
    [self.tabBar removeFromSuperview];
    
    tb.delegate =self;
}
/**
 *  根据一个类创建一个控制器
 *
 *  @param class         控制器对应的类
 *  @param title         标题
 *  @param image         默认状态的图片
 *  @param selectedImage 选择状态图片
 *
 *  @return 创建好的控制器
 */
- (UIViewController *)addViewControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selecterImage{

    UIViewController *vc = [[class alloc]init];
    
    return [self addViewControllerWithVc:vc title:title image:image selectedImage:selecterImage];
}
/**
 *  根据一个创建好的控制器初始化控制器
 *
 *  @param vc            控制器
 *  @param title         标题
 *  @param image         默认图片
 *  @param selectedImage 选中图片
 *
 *  @return 初始化之后的控制器
 */

- (UIViewController *)addViewControllerWithVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selecterImage{
    
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    UIImage *tempImage = [UIImage imageNamed:selecterImage];
    /*
     iOS7需要通过代码告诉系统不要用tabBar.tintColor来渲染选中图片
     */
    tempImage = [tempImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = tempImage;
//    vc.view.backgroundColor =RCrandColor;
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    RCNaviegationController *nav = [[RCNaviegationController alloc]initWithRootViewController:vc];
    
    [self addChildViewController:nav];
    //每添加一个控制器就添加一个对应的按钮
    
    [self.customTabBar addItem:vc.tabBarItem];
    
    
    return vc;
}

# pragma mark- RCtabBar的代理协议
- (void)tabBar:(RCtabBar *)tabBar selectedBtnFrom:(NSInteger)from to:(NSInteger)to{
    self.selectedIndex = to;
}
@end
